import { execute } from "../../database/connector";

import { AppropriationQueries } from "../queries/appropriation.query";
import { IAppropriation } from "../../models/appropriation.model";

/**
 * gets active appropriations
 */
export const getAppropriations = async () => {
  return execute<IAppropriation[]>(AppropriationQueries.GetAppropriations, []);
};

/**
 * gets approved appropriations
 */
export const getApprovedAppropriations = async () => {
  return execute<IAppropriation[]>(AppropriationQueries.GetApprovedAppropriations, []);
};

/**
 * gets a appropriation based on id provided
 */
export const getAppropriationById = async (id: IAppropriation['id']) => {
  return execute<IAppropriation>(AppropriationQueries.GetAppropriationById, [id]);
};

/**
 * adds a new active appropriation record
 */
export const insertAppropriation = async (appropriation: IAppropriation) => {
  const result = await execute<{ affectedRows: number }>(AppropriationQueries.AddAppropriation, [
    [Object.values(appropriation)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates appropriation information based on the id provided
 */
export const updateAppropriation = async (appropriation: IAppropriation) => {
  const result = await execute<{ affectedRows: number }>(AppropriationQueries.UpdateAppropriationById, [appropriation, appropriation.id]);
  return result.affectedRows > 0;
};

/**
 * updates appropriation information based on the id provided
 */
 export const deleteAppropriation = async (id: IAppropriation['id']) => {
  const result = await execute<{ affectedRows: number }>(AppropriationQueries.DeleteAppropriationById, [
    id
  ]);
  return result.affectedRows > 0;
};